home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / CTCPSTRE / CTCPSTRE.C next >
Text File  |  1990-06-28  |  7KB  |  320 lines

  1. /*
  2.     *** tcplib ***: TCP synchronous stream management utilities.
  3.                     Written by Matt Mashyna (Mostly) and Matt McNally,
  4.                         with original portions donated by Rob Chandok.
  5.                     
  6.                     All sorces ⌐ 1989 Carnegie Mellon University.
  7.                         
  8.                     Created: 08/29/89    Last Revised: 09/08/89 - Mashyna
  9.                     
  10.                     Adapted to Think C Objects 10/10/89 - Mashyna
  11. */
  12.  
  13.  
  14.  
  15. #include <Global.h>
  16. #include <Commands.h>
  17. #include <CApplication.h>
  18. #include <CBartender.h>
  19. #include <CDataFile.h>
  20. #include <CDecorator.h>
  21. #include <CDesktop.h>
  22. #include <CError.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "CTCPStream.h"
  26.  
  27. #define TIME_OUT 10                    /* default timeout period */
  28.  
  29. short    drvrRefNum = 0;
  30.  
  31. /*****
  32.  *
  33.  * Asynchonous event handler. Make your own -- not a method, just an example.
  34.  *
  35.  ****/
  36.  
  37. void             ASR(tcpStream, eventCode, userDataPtr, terminReason, icmpMsg)
  38. StreamPtr            tcpStream;
  39. unsigned short        eventCode;
  40. Ptr                 userDataPtr;
  41. unsigned short        terminReason;
  42. struct ICMPReport    *icmpMsg;
  43. {
  44.     printf("something happened: Eventcode %d\n",eventCode);
  45.     fflush(stdout);
  46.     Activity = eventCode;        /* tell the object that something happened */
  47. }
  48.  
  49. /*****
  50.  *
  51.  * needed for host resolution.
  52.  *
  53.  ****/
  54. pascal void dnrDone(struct hostInfo *hostInfoPtr, char *userDataPtr)
  55. {
  56.     #pragma unused(hostInfoPtr)
  57.  
  58.     (*userDataPtr)++;
  59. }
  60.  
  61. /*****
  62.  *
  63.  * returns a pointer to a resolved name from an address.
  64.  *
  65.  ****/
  66. char *inet_ntoa(ip_addr addr)
  67. {
  68.     char dnrdoneflag;
  69.     static struct hostInfo hi;
  70.     OSErr err;
  71.  
  72.     dnrdoneflag = 0; 
  73.     err = AddrToName(addr,&hi,&dnrDone,&dnrdoneflag);
  74.  
  75.     if (err == cacheFault) {
  76.         while (!dnrdoneflag) {
  77.             /* SystemTask? WaitNextEvent ? */
  78.         }
  79.         err = hi.rtnCode;
  80.     }
  81.     if (err == noErr) {
  82.         return hi.cname;
  83.     }
  84.     
  85.     /* if all else fails */
  86.     if ((err = AddrToStr(addr,hi.cname)) != noErr) {
  87.         /* doDNRerr(err); */
  88.         return "??";
  89.     } else {
  90.         return hi.cname;
  91.     }
  92. }
  93.  
  94. /*******************************************
  95. * tcplib utilities -                        *
  96. *                  bzero                        *
  97. *******************************************/
  98.  
  99.  
  100. void bzero(char * bytes, int len)
  101. {
  102.     register char *p;
  103.  
  104.     for (p = bytes; len > 0; len--,p++) {
  105.         *p = '\0';
  106.     }
  107. }   /*** end of bzero ***/
  108.  
  109.  
  110. /*******************************************
  111. * Release -                                *
  112. * Release & close a stream                   *
  113. *******************************************/
  114. void CTCPStream::Release()
  115. {
  116.     TCPiopb     tmp;
  117.     char         *buffPtr;
  118.     
  119.     bzero((char *)&tmp,sizeof(tmp));
  120.     
  121.     tmp.csCode            = TCPRelease;
  122.     tmp.tcpStream         = tcppb.tcpStream;
  123.     tmp.ioCRefNum         = tcppb.ioCRefNum;
  124.     
  125.     ourError = PBControl((ParmBlkPtr)&tmp,false);
  126.     
  127.     if(!ourError) {    
  128.         buffPtr = tmp.csParam.create.rcvBuff;
  129.         if(tmp.csParam.create.rcvBuffLen) free(buffPtr);
  130.         }
  131.     ourError = MemError();
  132.  
  133. }   /*** end of TCPReleaseStream ***/
  134.  
  135. void CTCPStream::Dispose()
  136. {
  137.     Release();
  138.     inherited::Dispose();
  139. }
  140.  
  141. /*******************************************
  142. * State         -                            *
  143. * check a stream state                       *
  144. *******************************************/
  145.  
  146. int CTCPStream::State()
  147. {
  148.     TCPiopb     tmp;
  149.     int            state        = 0;
  150.     
  151.     bzero((char *) &tmp,sizeof(tmp));
  152.     
  153.     if (tcppb.tcpStream != 0) {
  154.         
  155.         tmp.csCode     = TCPStatus;
  156.         tmp.tcpStream = tcppb.tcpStream;
  157.         tmp.ioCRefNum = tcppb.ioCRefNum;
  158.         
  159.         ourError = PBControl((ParmBlkPtr)&tmp,false);
  160.         if (ourError != noErr)
  161.             return(ourError);
  162.             
  163.         state = (int) tmp.csParam.status.connectionState;
  164.     }
  165.         
  166. return(state);
  167.  
  168. }   /*** end of TCPStreamState ***/
  169.  
  170.  
  171. /*******************************************
  172. * Close -                                    *
  173. * close a tcp connection;                    *
  174. *******************************************/
  175.  
  176. void CTCPStream::Close()
  177. {
  178.     ourError = noErr;
  179.  
  180.     if (tcppb.tcpStream != 0) {
  181.         
  182.         tcppb.ioCompletion                    = NIL;
  183.         tcppb.csCode                         = TCPClose;
  184.         tcppb.csParam.create.userDataPtr    = NIL;
  185.         
  186.         ourError = PBControl((ParmBlkPtr)&tcppb,false);
  187.     } /* if */
  188.     
  189. }   /*** end of CloseTCPStream ***/
  190.  
  191.  
  192. /*******************************************
  193. * ITCPStream -                                *
  194. * create a tcp connection; call this first *
  195. *******************************************/
  196.  
  197. void CTCPStream::ITCPStream(short size)
  198. {
  199.     IOBuffer = malloc(size);        /* allocate a buffer for the connection */
  200.     if (IOBuffer == NULL) 
  201.         ourError = MemError();
  202.  
  203.     
  204.     timeout        = TIME_OUT;
  205.     async        = false;
  206.     Activity    = 0;
  207.     
  208.     tcppb.csCode                     = TCPCreate;
  209.     tcppb.ioCompletion                = NIL;
  210.     tcppb.ioCRefNum                    = drvrRefNum;
  211.     tcppb.csParam.create.notifyProc    = NIL;
  212.     tcppb.csParam.create.rcvBuffLen    = size;
  213.     tcppb.csParam.create.rcvBuff    = IOBuffer;
  214.         
  215.     ourError = PBControl((ParmBlkPtr)&tcppb,false);
  216.  
  217. }   /*** end of CreateTCPStream ***/
  218.  
  219. /*************************************************
  220. * Listen -     [for ╘incoming╒ packets]             *
  221. * listen for a tcp connection; call this second  *
  222. *                                                 *
  223. * port is the port to listen on ie 79 is finger  *
  224. **************************************************/
  225.  
  226. void CTCPStream::Listen(tcp_port port)
  227. {
  228.             
  229.     tcppb.ioCompletion                = NIL;
  230.     tcppb.csCode                     = TCPPassiveOpen;
  231.     tcppb.csParam.open.remoteHost    = 0; 
  232.     tcppb.csParam.open.remotePort    = 0; 
  233.     tcppb.csParam.open.localPort    = thePort = port; 
  234.  
  235.     ourError = PBControl((ParmBlkPtr)&tcppb,true);
  236.  
  237. }   /*** end of ListenTCPSteam ***/
  238.  
  239.  
  240. /********************************************
  241. * Open - [for ╘outgoing╒ packets]            *
  242. * open a tcp connection; call this second    *
  243. *                                            *
  244. * host is a C string of the host to open.    *
  245. ********************************************/
  246.  
  247. void CTCPStream::Open(tcp_port port,char *host)
  248. {
  249.     char dnrdoneflag;
  250.     
  251.     ourError = StrToAddr(host,&hp,&dnrDone,&dnrdoneflag);
  252.     if (ourError == cacheFault) {
  253.         while (!dnrdoneflag) {
  254.             /* SystemTask? WaitNextEvent ? */
  255.             } /* while */
  256.         } /* if */
  257.     tcppb.ioCompletion                = NIL;
  258.     tcppb.csCode                     = TCPActiveOpen;
  259.     tcppb.csParam.open.remoteHost    = hp.addr[0];
  260.     tcppb.csParam.open.remotePort    = thePort = port;    /* eg. finger is 73 */
  261.  
  262.     ourError = PBControl((ParmBlkPtr)&tcppb,false);
  263.  
  264. }   /*** end of OpenTCPSteam ***/
  265.  
  266.  
  267. /*******************************************
  268. * Write -                                    *
  269. * write to a tcp Stream                       *
  270. *******************************************/
  271.  
  272. void CTCPStream::Write(char *srcBuff,int size)
  273. {
  274.     tcppb.csCode                         = TCPSend;
  275.     tcppb.csParam.send.ulpTimeoutValue    = timeout;
  276.     tcppb.csParam.send.ulpTimeoutAction = 1;
  277.  
  278.     /* build write-data structure */
  279.     
  280.     wds[0].length    = size;
  281.     wds[0].ptr        = srcBuff;
  282.     wds[1].length    = 0;
  283.         
  284.     tcppb.csParam.send.wdsPtr    = (Ptr)&wds;
  285.  
  286.     ourError = PBControl((ParmBlkPtr)&tcppb,async);
  287.     
  288. }   /*** end of WriteTCPStream ***/
  289.  
  290.  
  291.  
  292. /*******************************************
  293. * Read -                                    *
  294. * read from a tcp Stream                   *
  295. *******************************************/
  296.  
  297. void CTCPStream::Read(char *targBuff,int *size)
  298. {
  299.     tcppb.csCode                                 = TCPRcv;
  300.     tcppb.csParam.receive.commandTimeoutValue    = timeout; /* minimum time */
  301.     tcppb.csParam.receive.secondTimeStamp        = 0;
  302.     tcppb.csParam.receive.rcvBuff                = targBuff;
  303.     tcppb.csParam.receive.rcvBuffLen            = *size;
  304.  
  305.     ourError = PBControl((ParmBlkPtr)&tcppb,async);
  306.     *size = tcppb.csParam.receive.rcvBuffLen;
  307.  
  308. }   /*** end of ReadTCPStream ***/
  309.  
  310. /*******************************************
  311. * SetTimeOut -                                *
  312. * set the timeout value for reads & writes *
  313. ********************************************/
  314.  
  315. void CTCPStream::SetTimeOut(byte secs)
  316. {
  317.     timeout = secs;
  318. }
  319.  
  320.